package com.yeyks.commonReference.service.vip.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.dto.MemberCardGiftInfoDTO;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.MemberCardGiftInfoDownloadParam;
import com.yeyks.common.param.MemberCardGiftInfoParam;
import com.yeyks.common.param.MemberCardGiftUpdateParam;
import com.yeyks.common.result.MemberCardGiftDownloadResult;
import com.yeyks.common.utils.ArgumentsCheckUtil;
import com.yeyks.common.utils.DateUtils;
import com.yeyks.common.utils.ObjectConvertUtil;
import com.yeyks.common.utils.Splitters;
import com.yeyks.commonReference.service.vip.AccountVipSalerRelationService;
import com.yeyks.commonReference.service.vip.MemberCardGiftInfoService;
import com.yeyks.vip.dal.dao.AccountVipSalerRelationMapper;
import com.yeyks.vip.dal.dao.MemberCardGiftInfoMapper;
import com.yeyks.vip.dal.domain.MemberCardGiftInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 会员卡购买用户信息登记表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-08-07
 */
@Service
public class MemberCardGiftInfoServiceImpl extends ServiceImpl<MemberCardGiftInfoMapper, MemberCardGiftInfo> implements MemberCardGiftInfoService {

    @Autowired
    private AccountVipSalerRelationService accountVipSalerRelationService;

    @Override
    public AjaxResponseBody selectPageList(MemberCardGiftInfoParam param, AccountInfo loginUser) {
        Map<String, Object> paramMap = Maps.newHashMap();
        if (null != param) {
            paramMap = ObjectConvertUtil.objectToMap(param);
        }
        assert param != null;
        Date startDate = DateUtils.yyyyMMddToDate(param.getCreatedTime());
        assert paramMap != null;
        paramMap.put("startTime", startDate);
        if (null != startDate) {
            Date endDate = DateUtils.afterDaysToDate(startDate, 1L);
            paramMap.put("endTime", endDate);
        }
        List<Integer> statusList = null;
        if (ArgumentsCheckUtil.notEmpty(param.getStatus())) {
            statusList = Splitters.splitToInteger(param.getStatus(), Splitters.COMMA);
        }
        paramMap.put("list", statusList);
        if(loginUser.getType() != AccountType.Administrator){
            //得到下级的账号Id
            List<Integer> salerIdList = accountVipSalerRelationService.listByChildrenAndMySelf(loginUser);
            paramMap.put("salerIdList",salerIdList);
        }
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<MemberCardGiftInfoDTO> pageList = this.baseMapper.selectPageByParam(paramMap);
        PageInfo<MemberCardGiftInfoDTO> pageInfo = new PageInfo<>(pageList);
        return AjaxResponseBody.success(pageInfo);
    }

    @Override
    public List<MemberCardGiftDownloadResult> selectListByParam(MemberCardGiftInfoDownloadParam param, AccountInfo loginUser) {
        //状态结果集
        List<Integer> statusList = null;
        if (ArgumentsCheckUtil.notEmpty(param.getStatus())) {
            statusList = Splitters.splitToInteger(param.getStatus(), Splitters.COMMA);
        }
        List<Integer> salerIdList = null;
        if(loginUser.getType() != AccountType.Administrator){
            //得到下级的账号Id
            salerIdList = accountVipSalerRelationService.listByChildrenAndMySelf(loginUser);
        }
        List<MemberCardGiftInfoDTO> resultList = this.baseMapper.selectListByParam(param, statusList, salerIdList);
        List<MemberCardGiftDownloadResult> memberCardGiftDownloadResultList = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(resultList)) {
            resultList.forEach(memberCardGiftInfoDTO -> {
                MemberCardGiftDownloadResult downloadResult = new MemberCardGiftDownloadResult();
                downloadResult.setName(memberCardGiftInfoDTO.getName());
                downloadResult.setPhone(memberCardGiftInfoDTO.getPhone());
                downloadResult.setOrderNo(memberCardGiftInfoDTO.getOrderNo());
                downloadResult.setAddress(memberCardGiftInfoDTO.getAddress());
                downloadResult.setCreatedTime(DateUtils.formatYMDHMSStr(memberCardGiftInfoDTO.getCreatedTime()));
                if (memberCardGiftInfoDTO.getStatus() == 0) {
                    downloadResult.setStatus("待支付");
                } else if (memberCardGiftInfoDTO.getStatus() == 1) {
                    downloadResult.setStatus("待发货");
                } else if (memberCardGiftInfoDTO.getStatus() == 2) {
                    downloadResult.setStatus("已发货");
                }
                memberCardGiftDownloadResultList.add(downloadResult);
            });
        }
        return memberCardGiftDownloadResultList;
    }

    @Override
    public AjaxResponseBody confirmSendCard(MemberCardGiftUpdateParam param) {

        MemberCardGiftInfo memberCardGiftInfo = this.baseMapper.selectById(param.getId());
        if (null == memberCardGiftInfo) {
            throw new ServiceException("id无效");
        }
        memberCardGiftInfo.setStatus(2);
        int count = this.baseMapper.updateById(memberCardGiftInfo);
        if (count > 0) {
            return AjaxResponseBody.success("操作成功");
        }
        return AjaxResponseBody.failed("确认发货失败");
    }

}
